package com.hyt.it.ogt.kq.main.controller.gov;


import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.common.apiInfor.DataType;
import com.hyt.common.apiInfor.ParamType;
import com.hyt.exception.annotation.ThrowsAdvice;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.common.gov.config.swagger.ApiVersionConstant;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.gov.utils.tree.Tree;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.gov.model.param.BmBindParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.overview.ExamManageCollectVO;
import com.hyt.it.ogt.kq.service.gov.model.vo.overview.ExamMonitorCollectVO;
import com.hyt.it.ogt.kq.service.gov.service.ITaskOverviewService;
import com.hyt.it.ogt.kq.service.model.vo.candidateSubject.OverviewBmVO;
import com.hyt.swagger.ApiVersion;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 任务总览
 * </p>
 *
 * @author liuq
 * @since 2021-08-24
 */
@RestController
@Api(tags = "123.任务总览接口", value = "任务总览接口")
@ApiSort(value = 123)
@RequestMapping("/kw/gov/task/overview")
@Slf4j
public class TaskOverviewController extends BaseController {

    @Autowired
    private ITaskOverviewService iTaskOverviewService;

    @ApiOperation(
            value = "23.1 查询报名管理信息",
            notes = "30000：成功"
    )
    @ApiOperationSort(value = 1)
    @RequestMapping(value = "/bm/info", method = RequestMethod.GET)
    public ApiResponse<OverviewBmVO> getBmInfo(String taskId) {
        log.info("# 查询报名管理信息参数 ： {}", taskId);

        try {
            return ApiResponse.<OverviewBmVO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskOverviewService.getBmInfo(taskId, getOfficeId()))
                    .build();
        } catch (KqException i) {
            log.error("# 查询报名管理信息: {}", i.getErrMsg());
            return ApiResponse.<OverviewBmVO>builder()
                    .code(i.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 查询报名管理信息:", e);
            return ApiResponse.<OverviewBmVO>builder()
                    .code(ResponseCode.ERROR_OVERVIEW_QUERY_BM.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "23.2 查询云考试报名任务列表",
            notes = "30000：成功"
    )
    @ApiOperationSort(value = 2)
    @ApiVersion(group = {ApiVersionConstant.V2_4_10})
    @ThrowsAdvice(code = 43230201, print = "查询报名任务列表信息异常")
    @RequestMapping(value = "/bm/tree", method = RequestMethod.GET)
    public ApiResponse<List<Tree>> getBmTree() {
        return ApiResponse.<List<Tree>>builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iTaskOverviewService.getBmTree())
                .build();
    }

    @ApiOperation(
            value = "23.3 绑定云考试报名",
            notes = "30000：成功；34412：该任务已存在科目信息，不允许绑定报名；34412：该任务已存在批次信息，不允许绑定报名；34412：该任务已存在考生信息，不允许绑定报名"
    )
    @ApiOperationSort(value = 3)
    @ThrowsAdvice(code = 43230301, print = "任务绑定报名时异常")
    @RequestMapping(value = "/bm/bind", method = RequestMethod.POST)
    public ApiResponse<Object> bindBm(@RequestBody BmBindParam param) {
        log.info("# 绑定云考试报名参数 ： {}", JSON.toJSONString(param));

        iTaskOverviewService.bindBm(param);
        return ApiResponse.builder()
                .code(ResponseCode.SUCCESS.getCode())
                .build();
    }

    @ApiOperation(
            value = "23.4 解绑云考试报名",
            notes = "30000：成功"
    )
    @ApiOperationSort(value = 4)
    @RequestMapping(value = "/bm/unbind", method = RequestMethod.POST)
    public ApiResponse<Object> unbindBm(@RequestBody BmBindParam param) {
        log.info("# 解绑云考试报名 ： {}", param);

        try {
            String officeId = getOfficeId();
            iTaskOverviewService.unbindBm(param, officeId);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (KqException i) {
            log.error("# 绑定云考试报名异常: {}", i.getErrMsg());
            return ApiResponse.builder()
                    .code(i.getErrCode())
                    .build();
        } catch (Exception e) {
            log.error("# 绑定云考试报名异常:", e);
            return ApiResponse.builder()
                    .code(ResponseCode.ERROR_OVERVIEW_UNBIND.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "23.5 查询考试管理信息",
            notes = "30000：成功；24001：查询考试管理信息异常"
    )
    @ApiOperationSort(value = 5)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)
    })
    @RequestMapping(value = "/exam/manage", method = RequestMethod.GET)
    public ApiResponse<ExamManageCollectVO> getExamManage(@RequestParam(required = true) String taskId) {
        log.info("# 查询考试管理信息参数 ： {}", taskId);
        try {
            return ApiResponse.<ExamManageCollectVO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskOverviewService.getExamManageData(taskId))
                    .build();
        } catch (Exception e) {
            log.error("# 查询考试管理数据异常:", e);
            return ApiResponse.<ExamManageCollectVO>builder()
                    .code(ResponseCode.ERROR_OVERVIEW_QUERY_EXAM_MANAGE.getCode())
                    .build();
        }
    }

    @ApiOperation(
            value = "23.6 查询考试监控信息",
            notes = "30000：成功；24002：查询考试监控信息异常"
    )
    @ApiOperationSort(value = 6)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "taskId", value = "任务ID", dataType = DataType.STRING, paramType = ParamType.QUERY, required = true)})
    @RequestMapping(value = "/exam/monitor", method = RequestMethod.GET)
    public ApiResponse<ExamMonitorCollectVO> getExamMonitor(@RequestParam(required = true) String taskId, @RequestParam(required = false) String timeId) {
        log.info("# 查询考试监控信息参数 ： {}", taskId);
        try {
            return ApiResponse.<ExamMonitorCollectVO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iTaskOverviewService.getExamMonitorData(taskId, timeId))
                    .build();
        } catch (Exception e) {
            log.error("# 查询考试监控数据异常:", e);
            return ApiResponse.<ExamMonitorCollectVO>builder()
                    .code(ResponseCode.ERROR_OVERVIEW_QUERY_EXAM_MONITOR.getCode())
                    .build();
        }
    }

}
